﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using eLearning.Model;
using eLearning.Util;
using Microsoft.Phone.Controls;

namespace eLearning.View
{
    public partial class SettingPage : PhoneApplicationPage
    {
        private ObservableCollection<Menu> menus;

        public SettingPage()
        {
            InitializeComponent();

            Dictionary<string, Menu> settings = Setting.Instance.LoadMenuSetting();

            if (SavedUser.Instance.User.Type == User.UserType.Admin)
                menus = MenuAdminList.GetMenuList();
            else if (SavedUser.Instance.User.Type == User.UserType.Dosen)
                menus = MenuDosenList.GetMenuList();
            else
                menus = MenuMahasiswaList.GetMenuList();
            
            listMenu1.ItemsSource = menus;
            if (settings["Menu1"] != null)
                listMenu1.SelectedItem = settings["Menu1"];
            listMenu2.ItemsSource = menus;
            if (settings["Menu2"] != null)
                listMenu2.SelectedItem = settings["Menu2"];
            listMenu3.ItemsSource = menus;
            if (settings["Menu3"] != null)
                listMenu3.SelectedItem = settings["Menu3"];
            listMenu4.ItemsSource = menus;
            if (settings["Menu4"] != null)
                listMenu4.SelectedItem = settings["Menu4"];
            listMenu5.ItemsSource = menus;
            if (settings["Menu5"] != null)
                listMenu5.SelectedItem = settings["Menu5"];
        }


        private void SaveButton_Click(object sender, EventArgs e)
        {
            Menu menu1 = (Menu)listMenu1.SelectedItem;
            Menu menu2 = (Menu)listMenu2.SelectedItem;
            Menu menu3 = (Menu)listMenu3.SelectedItem;
            Menu menu4 = (Menu)listMenu4.SelectedItem;
            Menu menu5 = (Menu)listMenu5.SelectedItem;

            Dictionary<string, Menu> menus = new Dictionary<string, Menu>();
            menus.Add("Menu1", menu1);
            menus.Add("Menu2", menu2);
            menus.Add("Menu3", menu3);
            menus.Add("Menu4", menu4);
            menus.Add("Menu5", menu5);

            bool result = Setting.Instance.SaveMenuSetting(menus);
            if (result)
                MessageBox.Show("Setting has been saved");
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }
    }
}